package pl.edu.pjwstk.kor.sbql4j.project2;

import pl.wcislo.sbql4j.db4o.Db4oSBQLQuery;
import com.db4o.*;
import com.db4o.ObjectContainer;

import com.db4o.foundation.*;

import com.db4o.internal.*;
import com.db4o.internal.btree.*;

import org.apache.commons.collections.CollectionUtils;

import pl.edu.pjwstk.kor.database.Database;
import pl.edu.pjwstk.kor.database.Sbql4jQuery;
import pl.edu.pjwstk.kor.model.Customer;
import pl.edu.pjwstk.kor.model.Deal;
import pl.edu.pjwstk.kor.model.Employee;
import pl.edu.pjwstk.kor.model.Rank;

import pl.wcislo.sbql4j.exception.*;
import pl.wcislo.sbql4j.java.model.runtime.*;
import pl.wcislo.sbql4j.java.model.runtime.factory.*;
import pl.wcislo.sbql4j.java.utils.ArrayUtils;
import pl.wcislo.sbql4j.java.utils.OperatorUtils;
import pl.wcislo.sbql4j.java.utils.Pair;
import pl.wcislo.sbql4j.lang.codegen.nostacks.*;
import pl.wcislo.sbql4j.lang.codegen.simple.*;




import pl.wcislo.sbql4j.lang.parser.expression.*;
import pl.wcislo.sbql4j.lang.parser.expression.OrderByParamExpression.SortType;
import pl.wcislo.sbql4j.lang.parser.terminals.*;
import pl.wcislo.sbql4j.lang.parser.terminals.operators.*;
import pl.wcislo.sbql4j.lang.types.*;
import pl.wcislo.sbql4j.lang.xml.*;
import pl.wcislo.sbql4j.model.*;
import pl.wcislo.sbql4j.model.collections.*;
import pl.wcislo.sbql4j.util.*;
import pl.wcislo.sbql4j.util.Utils;
import pl.wcislo.sbql4j.xml.model.*;
import pl.wcislo.sbql4j.xml.parser.store.*;

import java.util.*;
import java.util.Collection;


public class Project2Query_SbqlQuery4Db4o0 implements Db4oSBQLQuery {
    private java.lang.Double dealCost;

    public Project2Query_SbqlQuery4Db4o0(java.lang.Double dealCost) {
        this.dealCost = dealCost;
    }

    /**
     * query='db.(Rank as r where ! count(r.getCustomers().getDeals().getCost()) > dealCost)'
    '
     **/
    public java.util.Collection<pl.edu.pjwstk.kor.model.Rank> executeQuery(
        final ObjectContainerBase ocb, final Transaction t) {
        final LocalTransaction transLocal = (LocalTransaction) t;
        final java.util.Collection<pl.edu.pjwstk.kor.model.Rank> _ident_Rank = new java.util.ArrayList<pl.edu.pjwstk.kor.model.Rank>();
        ClassMetadata _classMeta4 = ocb.classCollection()
                                       .getClassMetadata("pl.edu.pjwstk.kor.model.Rank");
        long[] _ids4 = _classMeta4.getIDs(transLocal);

        for (long _id4 : _ids4) {
            LazyObjectReference _ref4 = transLocal.lazyReferenceFor((int) _id4);
            _ident_Rank.add((pl.edu.pjwstk.kor.model.Rank) _ref4.getObject());
        }

        java.util.Collection<pl.edu.pjwstk.kor.model.Rank> _asResult_r = _ident_Rank;
        java.util.Collection<pl.edu.pjwstk.kor.model.Rank> _whereResult = new java.util.ArrayList<pl.edu.pjwstk.kor.model.Rank>();
        int _whereLoopIndex = 0;

        for (pl.edu.pjwstk.kor.model.Rank _whereEl : _asResult_r) {
            if (_whereEl == null) {
                continue;
            }

            if (_whereEl != null) {
                ocb.activate(_whereEl, 1);
            }

            pl.edu.pjwstk.kor.model.Rank _ident_r = _whereEl;

            if (_ident_r != null) {
                ocb.activate(_ident_r, 1);
            }

            pl.edu.pjwstk.kor.model.Rank _dotEl = _ident_r;

            if (_ident_r != null) {
                ocb.activate(_ident_r, 2);
            }

            java.util.List<pl.edu.pjwstk.kor.model.Customer> _mth_getCustomersResult =
                _dotEl.getCustomers();

            if (_mth_getCustomersResult != null) {
                ocb.activate(_mth_getCustomersResult, 2);
            }

            java.util.List<pl.edu.pjwstk.kor.model.Deal> _dotResult1 = new java.util.ArrayList<pl.edu.pjwstk.kor.model.Deal>();
            int _dotIndex1 = 0;

            for (pl.edu.pjwstk.kor.model.Customer _dotEl1 : _mth_getCustomersResult) {
                if (_dotEl1 == null) {
                    continue;
                }

                if (_dotEl1 != null) {
                    ocb.activate(_dotEl1, 1);
                }

                java.util.List<pl.edu.pjwstk.kor.model.Deal> _mth_getDealsResult =
                    _dotEl1.getDeals();

                if (_mth_getDealsResult != null) {
                    ocb.activate(_mth_getDealsResult, 2);
                }

                if (_mth_getDealsResult != null) {
                    ocb.activate(_mth_getDealsResult, 2);
                }

                _dotResult1.addAll(_mth_getDealsResult);
                _dotIndex1++;
            }

            java.util.List<java.lang.Double> _dotResult2 = new java.util.ArrayList<java.lang.Double>();
            int _dotIndex2 = 0;

            for (pl.edu.pjwstk.kor.model.Deal _dotEl2 : _dotResult1) {
                if (_dotEl2 == null) {
                    continue;
                }

                if (_dotEl2 != null) {
                    ocb.activate(_dotEl2, 1);
                }

                java.lang.Double _mth_getCostResult = _dotEl2.getCost();

                if (_mth_getCostResult != null) {
                    ocb.activate(_mth_getCostResult, 1);
                }

                if (_mth_getCostResult != null) {
                    ocb.activate(_mth_getCostResult, 1);
                }

                _dotResult2.add(_mth_getCostResult);
                _dotIndex2++;
            }

            java.lang.Integer _countResult = _dotResult2.size();
            java.lang.Double _ident_dealCost = dealCost;

            Boolean _moreResult = (_countResult == null) ? false
                                                         : (_countResult > _ident_dealCost);
            java.lang.Boolean _notResult = !_moreResult;

            if (_notResult) {
                _whereResult.add(_whereEl);
            }

            _whereLoopIndex++;
        }

        pl.wcislo.sbql4j.db4o.utils.DerefUtils.activateResult(_whereResult, ocb);

        return _whereResult;
    }
}
